package com.dhcc.sds.base.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.base.entity.SdsQceitemDicEntity;
import com.dhcc.sds.base.entity.SdsQcentityItemLogEntity;
import com.dhcc.sds.base.entity.SdsQcformItemEntity;
import com.dhcc.sds.base.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsQcentityItemEntity;


/**
 * 质控项目表
 * 
 * @ClassName: SdsQcentityItemController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:50
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcentityitem")
public class SdsQcentityItemController extends BaseController {

	private String PREFIX = "/backend/sds/sdsqcentityitem/";

	@Autowired
	private ISdsQcentityItemService sdsQcentityItemService;
	@Autowired
    private ISdsCommonService sdsCommonService;
	@Autowired
    private ISdsQcformItemService sdsQcformItemService;
	@Autowired
    private ISdsQcentityItemLogService sdsQcentityItemLogService;
	@Autowired
    private ISdsQceitemDicService sdsQceitemDicService;
	

    /**
     * 跳转到质控项目表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsqcentityitem.html";
    }
	
    /**
     * 跳转到新增质控项目表
     */
    @RequestMapping("/to_add/{entityId}")
    public String toAdd(@PathVariable Long entityId, Model model) {
        model.addAttribute("entityId",entityId);
        model.addAttribute("upTypeMap",sdsCommonService.getDicMap("UpType"));
        model.addAttribute("formItemMap",sdsQcformItemService.getFormItemMap(entityId));
        return PREFIX + "sdsqcentityitem_add.html";
    }
	
	/**
     * 跳转到修改质控项目表
     */
    @RequestMapping("/to_update/{entityId}/{sdsQcentityItemId}")
    public String toUpdate(@PathVariable Long entityId, @PathVariable Long sdsQcentityItemId, Model model) {
        SdsQcentityItemEntity sdsQcentityItem = this.sdsQcentityItemService.selectById(sdsQcentityItemId);
        model.addAttribute("sdsQcentityItem",sdsQcentityItem);
        model.addAttribute("upTypeMap",sdsCommonService.getDicMap("UpType"));
        model.addAttribute("formItemMap",sdsQcformItemService.getFormItemMap(entityId));
        return PREFIX + "sdsqcentityitem_edit.html";
    }

    /**
     * 获取质控项目表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
		Page<SdsQcentityItemEntity> page = new PageFactory<SdsQcentityItemEntity>().defaultPage();
        List<SdsQcentityItemEntity> result = this.sdsQcentityItemService.page(page,params);
        page.setRecords((List<SdsQcentityItemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增质控项目表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsQcentityItemEntity sdsQcentityItem) {
        if (CommonUtil.isOneEmpty(sdsQcentityItem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        // 获取当前登录人姓名(设置为公共属性)
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        Date updateDate = new Date();
        sdsQcentityItem.setUpdateUser(username);
        sdsQcentityItem.setUpdateDate(updateDate);
        sdsQcentityItem.setUpdateFlag(1);
        if (CommonUtil.isEmpty(sdsQcentityItem.getFormItemCode())) {
            sdsQcentityItem.setFormItemCode(sdsQcentityItem.getBtCode());
        }
        sdsQcentityItem.insert();
        //操作日志表插入日志
        Map<String,Object> param = new HashMap<>();
        param.put("type", 1);
        param.put("username", username);
        param.put("updateDate", updateDate);
        param.put("newData", sdsQcentityItem);
        param.put("oldData", null);
        sdsQcentityItemLogService.insertOperLog(param);

        return R.ok();
    }

    /**
     * 删除质控项目表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsQcentityItemId) {

        SdsQcentityItemEntity old = this.sdsQcentityItemService.selectById(sdsQcentityItemId);
	
        this.sdsQcentityItemService.deleteById(sdsQcentityItemId);

        //操作日志表插入日志
        // 获取当前登录人姓名(设置为公共属性)
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        Date updateDate = new Date();

        //操作日志表插入日志
        Map<String,Object> param = new HashMap<>();
        param.put("type", 3);
        param.put("username", username);
        param.put("updateDate", updateDate);
        param.put("newData", null);
        param.put("oldData", old);
        sdsQcentityItemLogService.insertOperLog(param);

        return R.ok();
    }

    /**
     * 修改质控项目表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsQcentityItemEntity sdsQcentityItem) {
        if (CommonUtil.isOneEmpty(sdsQcentityItem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcentityItemEntity old = this.sdsQcentityItemService.selectById(sdsQcentityItem.getId());
        SdsQcentityItemEntity entity = new SdsQcentityItemEntity();
        // 获取当前登录人姓名(设置为公共属性)
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        Date updateDate = new Date();

        BeanUtils.copyProperties(old, entity);
        old.setVersionId(sdsQcentityItem.getVersionId());
        old.setBtCode(sdsQcentityItem.getBtCode());
        old.setBtDesc(sdsQcentityItem.getBtDesc());
        old.setBtUpType(sdsQcentityItem.getBtUpType());
        old.setBtIsActive(sdsQcentityItem.getBtIsActive());
        old.setBtIsNeeded(sdsQcentityItem.getBtIsNeeded());
        old.setFormItemCode(sdsQcentityItem.getFormItemCode());
        old.setUpdateFlag(1);
        old.setUpdateUser(username);
        old.setUpdateDate(updateDate);
        old.updateAllColumnById();
        //操作日志表插入日志
        Map<String,Object> param = new HashMap<>();
        param.put("type", 2);
        param.put("username", username);
        param.put("updateDate", updateDate);
        param.put("newData", sdsQcentityItem);
        param.put("oldData", entity);
        sdsQcentityItemLogService.insertOperLog(param);

        return R.ok();
    }

    /**
     * 修改表单项目置为无效
     */
    @RequestMapping(value = "/setInvalidStatus")
    @ResponseBody
    public Object setInvalidStatus(@RequestParam Long sdsQcentityItemId) {
        SdsQcentityItemEntity old = this.sdsQcentityItemService.selectById(sdsQcentityItemId);

        SdsQcentityItemEntity entity = new SdsQcentityItemEntity();
        entity.setId(sdsQcentityItemId);
        entity.setBtIsActive(0);
        entity.setUpdateFlag(1);
        this.sdsQcentityItemService.updateById(entity);
        //操作日志表插入日志
        // 获取当前登录人姓名(设置为公共属性)
        User user = ShiroKit.getUser();
        String username = user.getLoginName();
        Date updateDate = new Date();
        Map<String,Object> param = new HashMap<>();
        param.put("type", 3);
        param.put("username", username);
        param.put("updateDate", updateDate);
        param.put("newData", null);
        param.put("oldData", old);
        sdsQcentityItemLogService.insertOperLog(param);
        return R.ok();
    }

    @RequestMapping(value="/resetUpdateFlag")
    @ResponseBody
    public Object resetUpdateFlag(@RequestParam Long sdsQcentityItemId){
        if (sdsQcentityItemId == null || sdsQcentityItemId == 0) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcentityItemEntity old = this.sdsQcentityItemService.selectById(sdsQcentityItemId);
        //重置标准项目更新标识
        old.setUpdateFlag(0);
        old.updateAllColumnById();
        //重置值域字典更新标识
        Wrapper<SdsQceitemDicEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("entity_item_id", old.getId());
        this.sdsQceitemDicService.updateForSet("update_flag=0",wrapper);
        return R.ok();
    }

    @RequestMapping(value="/synchro")
    @ResponseBody
    public Object synchro(@RequestParam Map<String, Object> params){
        this.sdsQcentityItemService.synchro(params);
        return R.ok();
    }
	
}
